package com.tiger.smartapp;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.youmi.android.AdListener;
import net.youmi.android.AdView;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.util.Log;
import android.view.Gravity;
import android.widget.FrameLayout;

public class CommonUtil {
	public static void alert(Activity act, String msg, String title) {
		AlertDialog.Builder builder = new AlertDialog.Builder(act);
		builder.setMessage(msg);
		if(title!=null)
			builder.setTitle(title);
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {

			}
		});
		builder.show();
	}
	/**
	 * 得到本地的apk
	 * @author:tiger.chenlin (2011-3-17) 
	 * @file:CommonUtil.java 
	 * @return:List<Map<String,String>>
	 * @return
	 */
	public List<Map<String, String>> getMyApk(){
		files.clear();
		File f = new File(this.rootDir);
		File[] file = f.listFiles(new FilenameFilter() {
			@Override
			public boolean accept(File dir, String name) {
				// TODO Auto-generated method stub
				return true;
			}
		});
		getFile(file);
		return files;
	}
	private void getFile(File fis[]) {
		for (File f : fis) {
//			Log.i(LOGTAG, f.getPath());
			if (f.getName().endsWith("."+this.fileType)){
				Map<String, String> tempFile=new HashMap<String, String>();
				tempFile.put("apkName", f.getName().replace("\n\r", ""));
				tempFile.put("apkPath", f.getPath());
				files.add(tempFile);
			}
			if(f.isFile())
				continue;
			if(f.isDirectory()){
				File[] ffile = f.listFiles(new FilenameFilter() {
					@Override
					public boolean accept(File dir, String name) {
						// TODO Auto-generated method stub
						return true;
					}
				});
				getFile(ffile);
			}
		}
	}
	List<Map<String, String>> files=new ArrayList<Map<String, String>>();
	String fileType="";
	String rootDir="";
	public CommonUtil(String rootDir,String fileType){
		this.fileType=fileType;
		this.rootDir=rootDir;
	}
	//卸载
	public static void uninstall(String packageName,Activity act){
		Uri uri = Uri.fromParts("package",packageName, null);
		Intent intent = new Intent(Intent.ACTION_DELETE , uri);
		act.startActivity(intent);  
	}
	public interface status{
		public int START=0;
		public int DO=1;
		public int END=2;
	}
	// 打开APK程序代码
	public static void install(File file,Activity act) {
		// TODO Auto-generated method stub
		Intent intent = new Intent();
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		intent.setAction(android.content.Intent.ACTION_VIEW);
		intent.setDataAndType(Uri.fromFile(file),
				"application/vnd.android.package-archive");
		act.startActivity(intent);
	}
	public static void setAdView(Activity act,AdListener listener) {
		// 初始化广告视图
		AdView adView = new AdView(act, Color.GRAY, Color.WHITE, 100);

		// 设置监听者(可以不设置，对获取广告没有影响)
		adView.setAdListener(listener);

		FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
				FrameLayout.LayoutParams.FILL_PARENT,
				FrameLayout.LayoutParams.WRAP_CONTENT);

		// 设置广告出现的位置(悬浮于底部)
		params.bottomMargin = 0;
		params.gravity = Gravity.BOTTOM;
		adView.setBackgroundColor(R.color.text_link_color);
		// 将广告视图加入Activity中
		act.addContentView(adView, params);
	}
}
